package com.supermap.hn.traffic.config;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ESConfig {
    public static String STR_INDEX = "vehicle_gps";

    public static String LOG_INDEX = "ssclog";

    @Value("${elasticsearch.scheme}")
    private String scheme;
    @Value("${elasticsearch.host}")
    private String host;
    @Value("${elasticsearch.port}")
    private int port = 9200;

    @Bean
    public RestHighLevelClient restHighLevelClient() {

        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, scheme)));
        boolean flag = CreateIndex(restHighLevelClient);
        CreateLogIndex(restHighLevelClient);
        if (flag) {
            return restHighLevelClient;
        } else {
            return null;
        }
    }

    private boolean CreateIndex(RestHighLevelClient restHighLevelClient) {
        GetIndexRequest getIndexRequest = new GetIndexRequest(STR_INDEX);
        try {
            boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            if(!exists){
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(STR_INDEX);
                Map<String, Object> properties = new HashMap<>();
                Map<String, Object> date = new HashMap<>();
                date.put("type", "date");
                properties.put("time", date);
                Map<String, Object> geo_point = new HashMap<>();
                geo_point.put("type", "geo_point");
                properties.put("gps", geo_point);
                Map<String, Object> source = new HashMap<>();
                source.put("properties", properties);
                createIndexRequest.mapping(source);
                CreateIndexResponse index = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
                System.out.println(index);
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private boolean CreateLogIndex(RestHighLevelClient restHighLevelClient) {
        GetIndexRequest getIndexRequest = new GetIndexRequest(LOG_INDEX);
        try {
            boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);
            if(!exists){
                CreateIndexRequest createIndexRequest = new CreateIndexRequest(LOG_INDEX);
                Map<String, Object> properties = new HashMap<>();

                //时间
                Map<String, Object> date = new HashMap<>();
                date.put("type", "date");
                properties.put("time", date);

                //GPS点
                Map<String, Object> geo_point = new HashMap<>();
                geo_point.put("type", "geo_point");
                properties.put("gps", geo_point);

                //车牌号
                Map<String, Object> vehicleNo = new HashMap<>();
                vehicleNo.put("type", "keyword");
                properties.put("vehicleNo", vehicleNo);

                //限速值
                Map<String, Object> limitSpeed = new HashMap<>();
                limitSpeed.put("type", "integer");
                properties.put("limitSpeed", limitSpeed);

                //IP地址
                Map<String, Object> ipAddr = new HashMap<>();
                ipAddr.put("type", "text");
                properties.put("ipAddr", ipAddr);

                //响应状态
                Map<String, Object> code = new HashMap<>();
                code.put("type", "text");
                properties.put("code", code);

                //响应时间
                Map<String, Object> handlingTime = new HashMap<>();
                handlingTime.put("type", "long");
                properties.put("handlingTime", handlingTime);


                Map<String, Object> source = new HashMap<>();
                source.put("properties", properties);
                createIndexRequest.mapping(source);
                CreateIndexResponse index = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);

            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
